package org.simple.spring;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.PropertyValue;

/**
 * 项目名称：spring-boot-learning
 * 类名称：BeanWrapperTest
 * 类描述：BeanWrapperTest
 * 创建时间：2020/3/18
 *
 * @author sisyphus   (E-mail:1620657419@qq.com)
 * @version v1.0
 */
public class BeanWrapperTest {

    public static void main(String[] args) {
        User user = new User();

        //通过BeanWrapper来修改JavaBean属性，需要在JavaBean中编写setter方法。
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(user);

        bw.setPropertyValue("name", "张三");
        System.out.println(user.getName());


        PropertyValue propertyValue = new PropertyValue("name2", "李四");
        bw.setPropertyValue(propertyValue);
        System.out.println(user.getName());
    }


    public static class User {

        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setName2(String name) {
            this.name = name + "2";
        }

    }


}
